<?php
require_once './phpthumb/ThumbLib.inc.php';
error_reporting(E_ERROR | E_PARSE);
 //Get Post Variables. The name is the same as 
//what was in the object that was sent in the jQuery
if (isset($_POST['sendValue'])){
    $value = $_POST['sendValue'];   
    $webcamimage=$_POST['webcamimage'];

}else{
    $value = "";
}

function getWithCurl($url)
{
    $curl = curl_init();
 
    // Setup headers - I used the same headers from Firefox version 2.0.0.6
    // below was split up because php.net said the line was too long. :/
    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: ";
    // browsers keep this blank.
 
    $referers = array("chatbot.in","google.com", "yahoo.com", "msn.com", "ask.com", "live.com");
    $choice = 1;//array_rand($referers);
    $referer = "http://" . $referers[$choice] . "";
 
    $browsers = array("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20060918 Firefox/2.0", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)");
    $choice2 = 0;//array_rand($browsers);
    $browser = $browsers[$choice2];
 
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, $browser);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_REFERER, $referer);
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 60);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 7);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
 
    $data = curl_exec($curl);
 
    if ($data === false) {
        $data = curl_error($curl);
    }
 
    // execute the curl command
    curl_close($curl);
    // close the connection
 
    return $data;
    // and finally, return $html
}
 
//$html= getWithCurl("http://www.google.com/searchbyimage?sbisrc=cr_1_0_0&image_url=http%3A%2F%2Fwww.ghacks.net%2Fwp-content%2Fuploads%2F2011%2F06%2Fsearch-google-with-this-image.png");
$html=getWithCurl($value);

function printTags($doc,$tag){ 

  $node = $doc->getElementsByTagName($tag);           
   for($c = 0; $c<$node->length; $c++){ 
     $text[$c] =$doc->saveXML($node->item($c)); 
     //echo $text[$c]; 

   } 

  
} 


$doc = new DOMDocument();
$doc->loadHTML($html);
//echo $doc->saveHTML();
//printTags($doc,"a");

$finder = new DomXPath($doc);
$classname="rg_r";
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), '$classname ')]");



$classname="bia";
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), '$classname ')]");

/*for($c = 0; $c<$nodes->length; $c++){ 
     $text[$c] =$doc->saveXML($nodes->item($c)); 
     echo $text[$c]; 

   }*/
   
   $text[$c] =$doc->saveXML($nodes->item($c)); 
 //    echo $text[$c]; 
     $src = $nodes->item($c)->getAttribute('href'); 
// echo "<iframe height=1000 width=800 src='http://www.google.com".$src."'/>";


$inp="http://www.google.com".$src;
$start=strpos($inp,"imgurl=")+7;
$end=strpos($inp,"&imgrefurl");
$retval=substr($inp,$start,$end-$start);


function GetImageFromUrl($link)
 
{
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_POST, 0);
 
curl_setopt($ch,CURLOPT_URL,$link);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$result=curl_exec($ch);
 
curl_close($ch);
 
return $result;
 
}

$sourcecode=GetImageFromUrl($retval);
$filename = 'googleimages/'.date('YmdHis') . '.jpg';
$savefile = fopen($filename, 'w');
fwrite($savefile, $sourcecode);
fclose($savefile);




try
{
     $thumb = PhpThumbFactory::create($filename);
}
catch (Exception $e)
{

     die('somethind went wrong!!');
}

$thumb->resize(400, 300);
$thumb->save($filename, 'png');


$destfilename=$filename;
$filename='http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/'.$filename;
//file_put_contents('test.txt', $webcamimage.'-'.$filename);

$webcamimage=substr($webcamimage,strpos($webcamimage,dirname($_SERVER['REQUEST_URI']))+strlen(dirname($_SERVER['REQUEST_URI']))+1);
$filename=substr($filename,strpos($filename,dirname($_SERVER['REQUEST_URI']))+strlen(dirname($_SERVER['REQUEST_URI']))+1);
//file_put_contents('test.txt', $webcamimage.'-'.$filename);


$src1 = imagecreatefromstring(file_get_contents($webcamimage)); 
$src2 = imagecreatefromstring(file_get_contents($filename)); 


// Create image instances
//$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(800, 300);

// Copy
imagecopy($dest, $src1, 0, 0, 0, 0, 400, 300);
imagecopy($dest, $src2, 400, 0, 0, 0, 400, 300);
// Output and free from memory
//header('Content-Type: image/gif');
//imagegif($dest);
imagepng($dest,$filename);
imagedestroy($dest);
imagedestroy($src1);
imagedestroy($src2);

file_put_contents('echo.txt', 'about to echo......');
echo json_encode(array("returnValue"=>$filename));  
file_put_contents('test.txt', 'echoed......');
unlink($webcamimage);
exit();
?>